package com.ufo.store.management.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.entity.SaleDetailInfo;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *  作者：taoli on 2017/7/18 0018 16:12
 *  
 */
public class StockSalesRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<SaleDetailInfo> mDetailList = new ArrayList<>();
    private int lastPosition = -1;

    public StockSalesRecyclerViewAdapter(Context context) {
        this.context = context;
    }


    public void addData(List<SaleDetailInfo> list){
        this.mDetailList.clear();
        this.mDetailList.addAll(list);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mDetailList.size();
    }

    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    /**
     * @desc 自定义动画
     * @author taoli
     * @time 2017/6/9 0009
     */
    private void setAnimation(View viewToAnimate, int position) {
        if (position > lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, 100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        } else if (position < lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, -100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        }
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
         TextView dateTv;
         TextView priceTv;
         TextView quantityTv;
        TextView totalTv;
        ProgressBar progressBar;

        public RecyclerViewHolder(View view) {
            super(view);
            dateTv = (TextView) view.findViewById(R.id.sale_date_tv);
            priceTv = (TextView) view.findViewById(R.id.sale_price_tv);
            quantityTv = (TextView) view.findViewById(R.id.sale_quantity_tv);
            totalTv = (TextView) view.findViewById(R.id.sale_total_tv);
            progressBar = (ProgressBar)view.findViewById(R.id.progressbar_pb);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.stock_sales_item, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final SaleDetailInfo info = mDetailList.get(position);
        if (info != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat format1 = new SimpleDateFormat("hh:mm");
                String dateStr = format.format(info.saleDetailOperdate) + " " + format1.format(info.saleDetailOpertime);
                holder1.dateTv.setText(dateStr);
                DecimalFormat decimalFormat = new DecimalFormat(".0");
                holder1.priceTv.setText(decimalFormat.format(info.saleDetailPrice) );
                holder1.quantityTv.setText(String.valueOf(info.saleDetailQuantity));
                holder1.totalTv.setText(decimalFormat.format(info.saleDetailQuantity * info.saleDetailPrice));


//                if( info.saleDetailCost > 0 ){
//                    float rate = ( info.saleDetailPrice - info.saleDetailCost ) / info.saleDetailCost;
//                    holder1.progressBar.setProgress( (int)(rate * info.saleDetailQuantity * 100f));
//                }else{
//                    holder1.progressBar.setProgress(0);
//                }
//                if( position % 4 == 0 ){
//                    Drawable draw = context.getResources().getDrawable(R.drawable.progress_horizontal_style_0);
//                    holder1.progressBar.setProgressDrawable(draw);
//                }else if( position % 4 == 1){
//                    Drawable draw = context.getResources().getDrawable(R.drawable.progress_horizontal_style_1);
//                    holder1.progressBar.setProgressDrawable(draw);
//                }else if( position % 4 == 2){
//                    Drawable draw = context.getResources().getDrawable(R.drawable.progress_horizontal_style_2);
//                    holder1.progressBar.setProgressDrawable(draw);
//                }else if( position % 4 == 3){
//                    Drawable draw = context.getResources().getDrawable(R.drawable.progress_horizontal_style_3);
//                    holder1.progressBar.setProgressDrawable(draw);
//                }
            }
        }

        //进入动画
        setAnimation(holder.itemView, position);

        //点击事件
        if (mListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(v, pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }
    }

    private OnClickListener mListener = null;

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
}
